package com.leyou.item.service;

import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.mapper.SpecParamMapper;
import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author shkstart
 * @create 2020-04-02 20:25
 */

@Service
public class SpecGroupService {
    @Autowired
    private SpecGroupMapper specGroupMapper;
    @Autowired
    private SpecParamMapper specParamMapper;

    public List<SpecGroup> querySpecGroupByCid(Long cid) {
        SpecGroup specGroup = new SpecGroup();
        specGroup.setCid(cid);
        return specGroupMapper.select(specGroup);
    }

    public int insertGroup(SpecGroup specGroup) {
        int i = specGroupMapper.insertSelective(specGroup);
        return i;
    }

    public int updateGroup(SpecGroup specGroup) {
        int i = specGroupMapper.updateByPrimaryKeySelective(specGroup);
        return i;
    }

    public int deleteByPKId(Long id) {
        int i = specGroupMapper.deleteByPrimaryKey(id);
        return i;
    }

    public List<SpecGroup> getGroupAndParam(Long cid) {
        SpecGroup specGroup = new SpecGroup();
        specGroup.setCid(cid);
        List<SpecGroup> select = specGroupMapper.select(specGroup);
        select.forEach(g->{
            SpecParam specParam = new SpecParam();
            specParam.setGroupId(g.getId());
            List<SpecParam> select1 = specParamMapper.select(specParam);
            g.setParams(select1);
        });
    return select;
    }
}
